home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / CD Audio Toolkit 1.0 / Source / CDPlayTrack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  5.7 KB  |  219 lines  |  [TEXT/MPS ]

  1. /*
  2.     CDPlayTrack - An XCMD to play an audio track.
  3.     ©Apple Computer, Inc. 1988
  4.     All Rights Reserved.
  5.     
  6.     88/10/08    BL°B    First Version
  7.  
  8.     To compile and link this file using Macintosh Programmer's Workshop,
  9.  
  10.     C -q2 CDPlayTrack.c
  11.     link -sn Main=CDPlayTrack -sn STDIO=CDPlayTrack ∂
  12.          -sn INTENV=CDPlayTrack -rt XFCN=42 ∂
  13.          -m CDPLAYTRACK CDPlayTrack.c.o "{CLibraries}"CRuntime.o ∂
  14.          "{CLibraries}"StdCLib.o ∂
  15.          -o HyperCommands
  16.          
  17.     This link directive puts the XCMD in the file "HyperCommands".
  18.     Substitute the name of the stack you want it in.  To move XCMDs
  19.     between stacks, use ResEdit.  They can be in an individual stack,
  20.     the Home stack, the HyperCard application, or the System File.
  21.     
  22. */
  23.  
  24. #include <cd.h>
  25.  
  26. /* prototype definitions for functions */
  27. void    ExtractTrackNo(char *, short *);
  28. OSErr    APlay(XCmdBlockPtr, short, short);
  29. OSErr    AStop(short, short);
  30.  
  31. /* **** WARNING:  DO NOT USE GLOBAL VARIABLES! **** */
  32.  
  33.  
  34. /************************************************************************
  35.  *
  36.  *  Function:        CDPlayTrack
  37.  *
  38.  *  Purpose:        play an audio track
  39.  *
  40.  *  Returns:        result of driver call to play track
  41.  *                    normally 0, but could have parameter error or
  42.  *                    other error if non-existent track is specified
  43.  *
  44.  *  Side Effects:
  45.  *
  46.  *  Description:    We need one parameter, the track number.  Get
  47.  *                    the global ioRefNum that we set by
  48.  *                    previously calling CDOpen().  Extract the 
  49.  *                    track number and play it.
  50.  *
  51.  *                    If we get a second parameter, it is the stop track.
  52.  *                    Issue a stop for that track.
  53.  *
  54.  ************************************************************************/
  55. pascal void
  56. CDPlayTrack(paramPtr)
  57.     XCmdBlockPtr    paramPtr;
  58. {
  59.     Str31    returnString;
  60.     OSErr    result;
  61.     short    trackNumber;
  62.     short    ioRefNum;
  63.     Handle    refHandle;
  64.     
  65.     /* Must be one parameter (or possibly two*/
  66.     if ((paramPtr->paramCount) != 1)
  67.         if ((paramPtr->paramCount) != 2)    
  68.         {
  69.             /* Report error in parameters by returning -1 */
  70.             NumToStr(paramPtr, (long) -1, &returnString);
  71.             paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  72.             return;
  73.         }
  74.     
  75.     /* Get the global ioRefNum and convert it. */
  76.     refHandle = GetGlobal(paramPtr, GLOBALNAME);
  77.     ioRefNum = atoi(*(refHandle));
  78.     DisposHandle(refHandle);
  79.     ioRefNum &= 0xFFFF;            /* remove vRefNum; not needed. */
  80.     
  81.     /* First param is track number.  Convert it to BCD */
  82.     ExtractTrackNo((char *)*(paramPtr->params[0]), &trackNumber);
  83.     
  84.     result = APlay(paramPtr, trackNumber, ioRefNum);
  85.     
  86.     if (paramPtr->paramCount == 2 && result == noErr)
  87.     {
  88.         /* Second param is stopping track number.  Convert it to BCD */
  89.         ExtractTrackNo((char *)*(paramPtr->params[1]), &trackNumber);
  90.         
  91.         result = AStop(trackNumber, ioRefNum);
  92.     }
  93.     
  94.     /* Convert result to string & return it */
  95.     NumToStr(paramPtr, (long) result, &returnString);
  96.     paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  97. }
  98.  
  99. /************************************************************************
  100.  *
  101.  *  Function:        ExtractTrackNo
  102.  *
  103.  *  Purpose:        Extract track number in BCD from PString
  104.  *
  105.  *  Returns:        nothing
  106.  *
  107.  *  Side Effects:    *track gets a new value
  108.  *
  109.  *  Description:    Extract track number in BCD from Cstring "name".
  110.  *                    "name" is always of the form "XX", where XX
  111.  *                    ranges from "1"  to "99"
  112.  *                    (You can't have more than 99 tracks on a compact disc.
  113.  *                    Bet you didn't know that, did you?  Right in the "Yellow
  114.  *                    Book" specification of compact disc encoding.)
  115.  *
  116.  ************************************************************************/
  117. void
  118. ExtractTrackNo(name, track)
  119. char        *name;
  120. short        *track;
  121. {
  122.     short    t;
  123.         
  124.     t = 0;
  125.     while (*name != 0)
  126.     {
  127.         t *= 16;
  128.         t += *name - '0';
  129.         name++;
  130.     }
  131.     
  132.     *track = t;    
  133. }
  134.  
  135. /************************************************************************
  136.  *
  137.  *  Function:        APlay
  138.  *
  139.  *  Purpose:        start playing an audio track
  140.  *
  141.  *  Returns:        OSErr.  Probably either
  142.  *                        noErr        everything's hunky-dory!
  143.  *                        paramErr    you messed up the call somehow.
  144.  *
  145.  *  Side Effects:    starts play.  By default, this will play until the
  146.  *                    end of the disc.
  147.  *
  148.  *  Description:    The track that you pass in is the first track that you
  149.  *                    want to have play.
  150.  *
  151.  ************************************************************************/
  152. OSErr
  153. APlay(paramPtr, track, refNum)
  154. XCmdBlockPtr    paramPtr;
  155. short    track;
  156. short    refNum;
  157. {
  158.     struct {
  159.         short    addrFormat;
  160.         long    address;
  161.         short    stopAddress;
  162.         short    playMode;
  163.     }    csParam;
  164.     short        playMode;
  165.     Handle        refHandle;
  166.     
  167.     /* Get the global ioRefNum and convert it. */
  168.     refHandle = GetGlobal(paramPtr, PLAYMODE);
  169.     playMode = atoi(*(refHandle));
  170.     DisposHandle(refHandle);
  171.     
  172.     csParam.addrFormat = TRACKADDR;
  173.     csParam.address = track;    /* must be in BCD */
  174.     csParam.stopAddress = 0;
  175.     csParam.playMode = playMode;
  176.     return (Control(refNum, APLAY, &csParam));
  177. }
  178.  
  179.  
  180. /************************************************************************
  181.  *
  182.  *  Function:        AStop
  183.  *
  184.  *  Purpose:        stop playing an audio track
  185.  *
  186.  *  Returns:        OSErr.  Probably either
  187.  *                        noErr        everything's hunky-dory!
  188.  *                        paramErr    you messed up the call somehow.
  189.  *
  190.  *  Side Effects:    stops play.
  191.  *
  192.  *  Description:    The track that you pass in is the LAST track that you
  193.  *                    want to have play.  This means that if you wanted to
  194.  *                    play only one track, you'd pass the same value to
  195.  *                    this routine and APlay() [q.v.]
  196.  *
  197.  *                    Note that this routine isn't called in this sample,
  198.  *                    but it's included for your enjoyment.
  199.  *
  200.  ************************************************************************/
  201. OSErr
  202. AStop(track, refNum)
  203. short    track;
  204. short    refNum;
  205. {
  206.     struct {
  207.         short    addrFormat;
  208.         long    address;
  209.     }    csParam;
  210.     
  211.     csParam.addrFormat = TRACKADDR;
  212.     csParam.address = track;
  213.     return (Control(refNum, ASTOP, &csParam));
  214. }
  215.  
  216.  
  217. /* C routines for HyperCard callbacks */
  218. #include <XCmdGlue.inc.c>
  219.